﻿/// <reference path="../jquery-1.4.4.js" />
/// <reference path="../../skin/default/jqueryUI/jquery.easyui.min.js" />


///***************************公用部分***************************//
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
//常规数据添加保存操作
//_url请求的地址 _msg提示的信息
function SetData(_url,_msg) {
    $(":checkbox").each(function () {
        $(this).next("input").val($(this).attr("checked"));
    });

    var url = _url;
    $.ajax({
        cache: false,
        url: url,
        type: 'POST',
        data: $("form").serializeArray(),
        beforeSend: function () {
            parent.layer.msg('正在提交信息，请等待……',{time: 1000});
        },
        success: function (data) {
            CheckCS00(data); //超时判断
            if (data == "true") {
                $(".layui-layer-content").remove();
                swal("提示信息", _msg, "success");

            }
            else {
                swal("提示信息", data.toString(), "error");
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //提示错误信息
            swal("提示信息", errorThrown, "error");
        },
        complete: function () {
            $('#mmXgmodal').modal('hide');
            $(".layui-layer-content").remove();
            $("#table_list_1").trigger("reloadGrid");
        }
    });
};

//_url请求的地址，_msg成功提示的信息，_成功后的执行函数
function SetDataExcFunction(_url, _msg, _callbackfunction) {

    $(":checkbox").each(function () {
        $(this).next("input").val($(this).attr("checked"));
    });
    var url = _url;
    $.ajax({
        cache: false,
        url: url,
        type: 'POST',
        data: $("form").serializeArray(),
        beforeSend: function () {
            parent.layer.msg('正在提交信息，请等待……');
        },
        success: function (data) {

            if (CS00(data)) //超时判断
            {
                //子窗口，要返回父级登录
                parent.window.location.href = "/Account/LogOn";
                return false;
            }
            if (data == "true") {
                swal("提示信息", _msg, "success");
                _callbackfunction();
                //window.parent.GetEmpCallback();
            }
            else {
                swal("提示信息", data.toString(), "error");
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //提示错误信息
            swal("提示信息", errorThrown, "error");
        },
        complete: function () {
            $('#mmXgmodal').modal('hide')
            $("#table_list_1").trigger("reloadGrid");
        }
    });
};
//删条 :删除ID，删除的地址
function Delete(ID, _url) {
    swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除！",
        cancelButtonText: "让我再考虑一下…",
        closeOnConfirm: false,
        closeOnCancel: false
    },
    function (isConfirm) {
        if (isConfirm) {
            $.post(_url, "ID=" + ID, function (data) {
                CheckCS00(data); //超时判断
                if (data == 'true') {
                    swal("删除成功！", "您已经永久删除了这条信息。", "success");
                    $("#table_list_1").trigger("reloadGrid");
                }
                else {
                    swal("提示信息", data, "error");
                }
            });

        } else {
            swal("已取消", "您取消了删除操作！", "error");
        }
    });
}


//删除多条 :ID名字，删除的地址，转向的地址
function Deletes(IDName, _url) {

    var IDs = $("#table_list_1").jqGrid('getCol', IDName, false)
    alert(IDs);
    swal({
        title: "您确定要删除选中的记录吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除！",
        cancelButtonText: "让我再考虑一下…",
        closeOnConfirm: false,
        closeOnCancel: false
    },
    function (isConfirm) {
        if (isConfirm) {
            $.post(_url, "IDs=" + IDs, function (data) {
                CheckCS00(data); //超时判断
                if (data == 'true') {
                    swal("删除成功！", "您已经永久删除了选中的记录。", "success");
                    $("#table_list_1").trigger("reloadGrid");
                }
                else {
                    swal("提示信息", data, "error");
                }
            });

        } else {
            swal("已取消", "您取消了删除操作！", "error");
        }
    });

}
//检查启用禁用状态
function CheckStatus(_url, ID, status) {
    var Info = '';
    if (status == "0") {
        Info = '禁用';
    } else {
        Info = '启用';
    }
    swal({
        title: "确认信息",
        text: "你是否确认“" + Info + "”吗？",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要“" + Info + "”！",
        cancelButtonText: "让我再考虑一下…",
        closeOnConfirm: false,
        closeOnCancel: false
    },
    function (isConfirm) {
        if (isConfirm) {
        $.post(_url, "ID=" + ID + "&status=" + status, function (data) {

            if (data == '1') {
                swal("操作成功！", "您已经成功更改状态", "success");
                $("#table_list_1").trigger("reloadGrid");
            }
            else {
                swal("提示信息", data, "error");
            }
        });

    } else {
        swal("已取消", "您取消了操作！", "error");
    }
});
}


function CS00(_value) {
    if (_value.indexOf("CS00") > -1) {
        if (_value.substring(0, 4) == "CS00") {
            return true;
        }
    }
    return false;
}
//异步请求时的超时跳转
function CheckCS00(_value) {
    if (_value.indexOf("CS00") > -1) {
        if (_value.substring(0, 4) == "CS00") {
            if (window.parent != null) {
                window.parent.location.href = "/Account/LogOn";
            }
            else {
                window.location.href = "/Account/LogOn";
            }
            return false;
        }
    }
}

//清空文本框内容
function clearForm(form) {
    // input清空
    $(':input', form).each(function () {
        var type = this.type;
        var tag = this.tagName.toLowerCase(); // normalize case
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = "";
            // 多选checkboxes清空
            // select 下拉框清空
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};


